/**
 * 下午1:38:10
 */
package com.hsnn.medstgmini.pro.project.dao.impl;

import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.hsnn.medstgmini.base.std.model.StdCompprofile;
import com.hsnn.medstgmini.dao.HibernateSqlUtil;
import com.hsnn.medstgmini.pro.project.dao.BidProjcompprofileDao;
import com.hsnn.medstgmini.util.Pagination;

/**
 * @author hsnn
 *
 */
@Repository
public class BidProjcompprofileDaoImpl implements BidProjcompprofileDao {

	@Resource(name = "sessionFactory")
	private SessionFactory sessionFactory;
	
	@Autowired
	private HibernateSqlUtil dao;
	private Session getSession() {
		Session session = sessionFactory.getCurrentSession();
		if (null == session) {
			session = sessionFactory.openSession();
		}
		return session;
	}
	/**
	 * @category 根据企业获取项目列表
	 * @author 应晓川
	 * @date   2015年8月27日10:03:40
	 * @param  @return
	 */
	@Override
	public Pagination getBidProjectList(Pagination page) {
		StringBuffer sql = new StringBuffer()
		.append(" select bp.proj_id projId, ")
		.append(" bp.proj_code projCode, ")
		.append(" bp.name,  ")
		.append(" bp.proj_intro projIntro, ")
		.append(" bp.starttime,  ")
		.append(" bp.endtime,  ")
		.append(" ds.dic_name projCat, ")
		.append(" bp.remark,  ")
		.append(" bp.status,  ")
		.append(" bp.upd_datetime updDatetime ")
		.append(" from  bid_project bp left join dic_sysdic ds on ds.dic_code=bp.proj_cat ")
		.append("  where ds.classify_code=116 and ds.status=1 ");
		
		if(StringUtils.isNotBlank(page.getConditions().get("projCode")) ){
			sql.append("  and bp.proj_code = :projCode ");
		}
		if(StringUtils.isNotBlank(page.getConditions().get("name")) ){
			sql.append("  and bp.name like :name ");
			page.getConditions().put("name", "%"+page.getConditions().get("name")+"%");
		}
		sql.append(" order by bp.starttime desc,bp.status desc ");
		return dao.getSqlPageMapList(sql.toString(), page.getConditions(), page);
	}
	
	/**
	 * @category 根据项目获取企业资质列表
	 * @author 应晓川 修改人：韩守松
	 * @date   2015年8月27日10:05:46 修改日期 2015-09-23
	 * @param  @return
	 */
	@Override
	public Pagination getBidProjcompprofileList(Pagination page) {
		StringBuffer sql = new StringBuffer()
		.append(" select sc.compprofile_id compprofileId, ")
		.append(" sc.comp_id compId, ")
		.append(" sc.profile_code profileCode, ")
		.append(" sc.profile_name profileName, ")
		.append(" sc.profile_type profileType, ")
		.append(" DATE_FORMAT(sc.profile_starttime,'%Y-%m-%d') profileStarttime, ")
		.append(" DATE_FORMAT(sc.profile_endtime,'%Y-%m-%d')  profileEndtime, ")
		.append(" sc.profile_pic_addr profilePicAddr, ")
		.append(" sc.profile_pic_shortcut_addr profilePicShortcutAddr, ")
		.append(" sc.status , ")
		.append(" sc.remarks ")
		.append(" from std_compprofile sc ")
		.append(" where sc.comp_id = :compId ")
		.append(" and sc.audit_status = 1 ")
		.append(" and sc.status = 1 ")
		.append(" and sc.compprofile_id not in ( ")
		.append(" select bp.compprofile_id from bid_projcompprofile bp  where bp.proj_id = :projId and bp.audit_status != 3 and bp.status =1 ) ")
		.append(" order by sc.upd_datetime desc ");
		return dao.getSqlPageMapList(sql.toString(), page.getConditions(), page);
	}

	/* (non-Javadoc)
	 * @see com.hsnn.medstgmini.pro.project.dao.BidProjcompprofileDao#getBidProjcompprofileListByName(java.lang.String)
	 */
	@Override
	public List<StdCompprofile> getBidProjcompprofileListByName(String name) {
		Query query = getSession().createQuery("from StdCompprofile where profileName='"+name+"'");
		@SuppressWarnings("unchecked")
		List<StdCompprofile> list= query.list();
		return list;
	}
	
	/**
	 * @category 获取企业资质审核结果列表
	 * @author 应晓川
	 * @date  2015年8月31日10:41:47
	 * @param  @return
	 */
	@Override
	public Pagination getBidProjcompprofileResultList(Pagination page) {
		StringBuffer sql = new StringBuffer()
		.append(" select sc.compprofile_id compprofileId, ")
		.append(" bpc.proj_compprofile_id projCompprofileId, ")
		.append(" sc.profile_code profileCode, ")
		.append(" sc.profile_name profileName, ")
		.append(" sc.profile_type profileType, ")
		.append(" DATE_FORMAT(sc.profile_starttime,'%Y-%m-%d') profileStarttime, ")
		.append(" DATE_FORMAT(sc.profile_endtime,'%Y-%m-%d')  profileEndtime, ")
		.append(" sc.profile_pic_addr profilePicAddr, ")
		.append(" sc.profile_pic_shortcut_addr profilePicShortcutAddr, ")
		.append(" bpc.audit_status auditStatus, ")
		.append(" bpc.audit_remark auditRemark, ")
		.append(" bpc.complain_status complainStatus, ")
		.append(" bpc.complain_audit_remark complainAuditRemark, ")
		.append(" bpc.complain_audit_datetime complainAuditDatetime, ")
		.append(" DATE_FORMAT(bpc.complain_datetime,'%Y-%m-%d  %H:%i:%S')  complainDatetime, ")
		.append(" DATE_FORMAT(bpc.audit_datetime ,'%Y-%m-%d %H:%i:%S') auditDatetime ")
		.append(" from bid_projcompprofile bpc left join std_compprofile sc on sc.compprofile_id=bpc.compprofile_id ")
		.append(" where sc.comp_id = :compId and bpc.proj_id = :projId and bpc.status=1 and sc.status =1 and sc.audit_status =1 ");
		/*if (StringUtils.isNotBlank(page.getConditions().get("auditStatus"))) {
			if(page.getConditions().get("auditStatus").equals("2")){//查审核未通过的，目的是去澄清
				sql.append(" and bpc.apply_type=1 and bpc.audit_status!=0 and bpc.audit_status =:auditStatus ");	
			}else{
				sql.append(" and  bpc.apply_type=1 and bpc.audit_status!=:auditStatus ");//获取企业资质审核结果列表数据
			}
			sql.append(" order by bpc.audit_datetime desc ");
		}else if (StringUtils.isNotBlank(page.getConditions().get("complainStatus"))) {//获取企业资质澄清结果列表数据
			sql.append(" and bpc.apply_type=0 and bpc.complain_status!=:complainStatus ");
			sql.append(" order by bpc.complain_datetime desc ");
		}else{
			
		}*/
		if (StringUtils.isNotBlank(page.getConditions().get("auditStatus"))) {
			if(page.getConditions().get("auditStatus").equals("2")){//查审核未通过的，目的是去澄清
				sql.append(" and bpc.audit_status !=3 and bpc.audit_status =:auditStatus ");	
			}else{
				sql.append("  and bpc.audit_status!=:auditStatus ");//获取企业资质审核结果列表数据
			}
		}
		if (StringUtils.isNotBlank(page.getConditions().get("complainStatus"))) {
			if(page.getConditions().get("complainStatus").equals("3")){//查审核未通过的，目的是去澄清
				sql.append("  and bpc.audit_status = 2 and bpc.complain_status != :complainStatus ");	
			 }
		}
		sql.append(" order by sc.upd_datetime desc ");	
		return dao.getSqlPageMapList(sql.toString(), page.getConditions(),page);
	}
	
	/**
	 * @category 获取企业资质申报结果列表
	 * @author 应晓川
	 * @date  2015年8月31日10:41:47
	 * @param  @return
	 */
	@Override
	public Pagination getBidProjcompprofileApplyResultList(Pagination page) {
		StringBuffer sql = new StringBuffer()
		.append(" select sc.compprofile_id compprofileId, ")
		.append(" bpc.proj_compprofile_id projCompprofileId, ")
		.append(" sc.profile_code profileCode, ")
		.append(" sc.profile_name profileName, ")
		.append(" sc.profile_type profileType, ")
		.append(" DATE_FORMAT(sc.profile_starttime,'%Y-%m-%d') profileStarttime, ")
		.append(" DATE_FORMAT(sc.profile_endtime,'%Y-%m-%d')  profileEndtime, ")
		.append(" sc.profile_pic_addr profilePicAddr, ")
		.append(" sc.profile_pic_shortcut_addr profilePicShortcutAddr, ")
		.append(" bpc.audit_status auditStatus, ")
		.append(" bpc.audit_remark auditRemark, ")
		.append(" bpc.complain_status complainStatus, ")
		.append(" bpc.complain_audit_remark complainAuditRemark, ")
		.append(" bpc.complain_audit_datetime complainAuditDatetime, ")
		.append(" DATE_FORMAT(bpc.complain_datetime,'%Y-%m-%d  %H:%i:%S')  complainDatetime, ")
		.append(" DATE_FORMAT(bpc.audit_datetime ,'%Y-%m-%d %H:%i:%S') auditDatetime ")
		.append(" from bid_projcompprofile bpc left join std_compprofile sc on sc.compprofile_id=bpc.compprofile_id ")
		.append(" where sc.comp_id = :compId and bpc.proj_id = :projId and bpc.status=1 and bpc.audit_status != 3 and sc.status =1 and sc.audit_status =1  ");
		return dao.getSqlPageMapList(sql.toString(), page.getConditions(),page);
	}

	
	@Override
	public Pagination getProjCompprofile(Pagination page) {
		StringBuffer sql = new StringBuffer()
		.append(" select bp.proj_id projId, ")
		.append(" bp.proj_code projCode, ")
		.append(" bp.name,  ")
		.append(" bp.proj_intro projIntro, ")
		.append(" bp.starttime,  ")
		.append(" bp.endtime,  ")
		.append(" ds.dic_name projCat, ")
		.append(" bp.remark,  ")
		.append(" bp.status,  ")
		.append(" bp.upd_datetime updDatetime ")
		.append(" from  bid_project bp left join dic_sysdic ds on ds.dic_code=bp.proj_cat ")
		.append("  where ds.classify_code=116 and ds.status=1 ");
		if(StringUtils.isNotBlank(page.getConditions().get("projCode")) ){
			sql.append("  and bp.proj_code = :projCode ");
		}
		if(StringUtils.isNotBlank(page.getConditions().get("name")) ){
			sql.append("  and bp.name like :name ");
			page.getConditions().put("name", "%"+page.getConditions().get("name")+"%");
		}
		sql.append(" order by bp.starttime desc,bp.status desc ");
		return dao.getSqlPageMapList(sql.toString(), page.getConditions(), page);
	}
	
	@Override
	public Pagination getProjAuditResult(Pagination page) {
		// TODO Auto-generated method stub
		return null;
	}
	
	@Override
	public Pagination getProjComplain(Pagination page) {
		// TODO Auto-generated method stub
		return null;
	}
	
	@Override
	public Pagination getProjComplainResult(Pagination page) {
		// TODO Auto-generated method stub
		return null;
	}

}
